#!/usr/bin/env python3
# -*- coding: utf-8 -*-

' 用于自动登录猫耳'

__author__ = 'Lucky Xiong'

import time
from selenium.webdriver import ActionChains


def login(user, pwd, driver,*, model='save', url='https://www.missevan.com/member/login'):
    driver.get(url)


  #  username = driver.find_element_by_xpath('//*[@id="username"]/input').send_keys("hello")
  #  password = driver.find_element_by_xpath('//*[@id="password"]/input').send_keys("hello")
    #验证滑块
   # slide(driver,348)
    #点击登录
  #  logbut = driver.find_element_by_xpath('//*[@id="login"]/div[2]/form/div[5]/button').click()

    '''改为第三方登录模式'''
    time.sleep(4)
    # 计时退出
    time_start = time.time()
    # 设定登录超时时间 second
    timeout = 2000
    while True:
        URL = driver.current_url
     #   print(URL)
        time.sleep(1)
        if URL == 'https://www.missevan.com/':
            break
        if time.time()-time_start > timeout:
            print('登录超时了， 妹儿。重来吧')
            break
    '''判断是否登录成功
    注册/登陆
    //*[@id="header"]/div/ul[2]/li[2]/a
    '''
    while True:
        textBom = driver.find_element_by_xpath('//*[@id="header"]/div/ul[2]/li[2]/a').text
        print(textBom)
        if textBom == '登录/注册':
            print('请重新登陆')
        else:
            print('欢迎你，%s. 老子爱你。' % textBom)
            break

    return driver

# """过滑块验证
#     1366分辨率下 猫耳 348 px .
# """
# def slide(driver,distance):
#     #获取滑块
#     slide = driver.find_element_by_id("nc_1_n1z")
#     print('获取的滑块信息：',slide)
#     action = ActionChains(driver)
#     action.click_and_hold(slide).perform()  # perform()用来执行ActionChains中存储的行为
#     for x in get_track(distance):
#         action.move_by_offset(xoffset=x,yoffset=0).perform()
#     time.sleep(0.5)
#     action.release().perform()
#
# '滑动轨迹'
# ''' 先加速后减速的运动轨迹
# https://www.zhihu.com/question/32209043/answer/229138450
# '''
# def get_track(distance):
#     '''
#     根据偏移量获取移动轨迹
#     ： param distance: 偏移量
#     ： return ： 偏移轨迹
#     '''
#     # 移动轨迹
#     track = []
#     # 当前位移
#     current = 0
#     # 减速阈值
#     mid = distance * 3 / 4
#     # 计算间隔
#     t = 0.2
#     # 初速度
#     v = 0
#     while current < distance:
#         if current < mid:
#             # 加速度
#             a = 2
#         else:
#             # 加速度
#             a = -3
#         # 初速度
#         v0 = v
#         # 当前速度
#         v = v0 + a * t
#         # 移动距离
#         move = v0 * t + 1 / 2 * a * t * t
#         current += move
#         # 加入轨迹
#         track.append(round(move))
#     return track
